﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Globalization;
using Excel.Expressions.Interfaces;
using Expressions.Compiler;

namespace Excel.Expressions.Tests
{
    /// <summary>
    /// Summary description for Grammar
    /// </summary>
    [TestClass]
    public class Grammar
    {
        public Grammar()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        private Entity CreateGrammar1()
        {
            // Forward declarations
            Nonterminal real_literal = "real-literal";
            Nonterminal exponent_part = "exponent-part";
            Nonterminal sign = "sign";
            Nonterminal real_type_suffix = "real-type-suffix";
            Nonterminal decimal_digits = "decimal-digits";
            Nonterminal decimal_digit = "decimal-digit";

            /*
            real-literal::
                sign[opt] decimal-digits . decimal-digits exponent-part[opt] real-type-suffix[opt]
                sign[opt] . decimal-digits exponent-part[opt] real-type-suffix[opt] 
                sign[opt] decimal-digits exponent-part real-type-suffix[opt] 
                sign[opt] decimal-digits real-type-suffix 
            */
            real_literal.Definition.Rules = new Rule.Stream
            {
                () => sign[true] + decimal_digits[false] + '.' + decimal_digits + exponent_part[true] + real_type_suffix[true],
                () => sign[true] + '.' + decimal_digits[false] + exponent_part[true] + real_type_suffix[true],
                () => sign[true] + decimal_digits[false] + exponent_part + real_type_suffix[true],
                () => sign[true] + decimal_digits[false] + real_type_suffix[true]
            };

            /*
            exponent-part::
                e sign[opt] decimal-digits 
                E sign[opt] decimal-digits 
            */
            exponent_part.Definition.Rules = new Rule.Stream
            {
                () => 'e' + sign[true] + decimal_digits,
                () => 'E' + sign[true] + decimal_digits
            };

            /*
            sign:: one of
                + - 
            */
            sign.Definition.Rules = new Rule.Stream
            {
                () => '+',
                () => '-'
            };

            /*
            real-type-suffix:: one of
                F f  
             */
            real_type_suffix.Definition.Rules = new Rule.Stream
            {
                () => 'F',
                () => 'f'
            };

            /*
            decimal-digits::
                decimal-digit 
                decimal-digits decimal-digit 
            */
            decimal_digits.Definition.Rules = new Rule.Stream
            {
                () => decimal_digit + decimal_digits[true]
            };

            /*
            decimal-digit:: one of
                0 1 2 3 4 5 6 7 8 9 
            */
            decimal_digit.Definition.Rules = new Rule.Stream
            {
                () => '0',
                () => '1',
                () => '2',
                () => '3',
                () => '4',
                () => '5',
                () => '6',
                () => '7',
                () => '8',
                () => '9'
            };

            Nonterminal E = "E";
            Nonterminal T = "T";
            Nonterminal F = "F";
            Nonterminal N = "N";

            Nonterminal AE = "+";
            Nonterminal SE = "-";
            Nonterminal MT = "*";
            Nonterminal DT = "/";

            E.Definition.Rules = new Rule.Stream
            {
                () => T + AE[false],
                () => T + SE[false],
                () => T
            };

            T.Definition.Rules = new Rule.Stream
            {
                () => F + MT[false],
                () => F + DT[false],
                () => F
            };

            F.Definition.Rules = new Rule.Stream
            {
                () => N,
                () => '(' + E + ')'
            };

            N.Definition.Rules = new Rule.Stream
            {
                () => real_literal,  
            };

            AE.Definition.Rules = new Rule.Stream
            {
                () => '+' + E[false]
            };

            SE.Definition.Rules = new Rule.Stream
            {
                () => '-' + E[false]
            };

            MT.Definition.Rules = new Rule.Stream
            {
                () => '*' + T[false]
            };

            DT.Definition.Rules = new Rule.Stream
            {
                () => '/' + T[false]
            };

            real_literal.Definition.Eval = (input, tree) => double.Parse(tree.Context.Text, CultureInfo.InvariantCulture.NumberFormat);

            Func<double, double, double> add = (a, b) => a + b;
            Func<double, double, double> sub = (a, b) => a - b;
            Func<double, double, double> mul = (a, b) => a * b;
            Func<double, double, double> div = (a, b) => a / b;

            AE.Definition.Eval = (input, tree) =>
            {
                return add(input, E[tree]);
            };

            SE.Definition.Eval = (input, tree) =>
            {
                return sub(input, E[tree]);
            };

            MT.Definition.Eval = (input, tree) =>
            {
                return mul(input, T[tree]);
            };

            DT.Definition.Eval = (input, tree) =>
            {
                return div(input, T[tree]);
            };

            Nonterminal grammar = "G";

            grammar.Definition.Rules = new Rule.Stream 
            {
                () => E[false] + Terminal.Null
            };

            return grammar;
        }


        private Entity CreateGrammar2()
        {
            // Forward declarations
            Nonterminal real_literal = "real-literal";
            Nonterminal exponent_part = "exponent-part";
            Nonterminal sign = "sign";
            Nonterminal real_type_suffix = "real-type-suffix";
            Nonterminal decimal_digits = "decimal-digits";
            Nonterminal decimal_digit = "decimal-digit";

            /*
            real-literal::
                sign[opt] decimal-digits . decimal-digits exponent-part[opt] real-type-suffix[opt]
                sign[opt] . decimal-digits exponent-part[opt] real-type-suffix[opt] 
                sign[opt] decimal-digits exponent-part real-type-suffix[opt] 
                sign[opt] decimal-digits real-type-suffix 
            */
            real_literal.Definition.Rules = new Rule.Stream
            {
                () => sign[true] + decimal_digits[false] + '.' + decimal_digits + exponent_part[true] + real_type_suffix[true],
                () => sign[true] + '.' + decimal_digits[false] + exponent_part[true] + real_type_suffix[true],
                () => sign[true] + decimal_digits[false] + exponent_part + real_type_suffix[true],
                () => sign[true] + decimal_digits[false] + real_type_suffix[true]
            };

            /*
            exponent-part::
                e sign[opt] decimal-digits 
                E sign[opt] decimal-digits 
            */
            exponent_part.Definition.Rules = new Rule.Stream
            {
                () => 'e' + sign[true] + decimal_digits,
                () => 'E' + sign[true] + decimal_digits
            };

            /*
            sign:: one of
                + - 
            */
            sign.Definition.Rules = new Rule.Stream
            {
                () => '+',
                () => '-'
            };

            /*
            real-type-suffix:: one of
                F f  
             */
            real_type_suffix.Definition.Rules = new Rule.Stream
            {
                () => 'F',
                () => 'f'
            };

            /*
            decimal-digits::
                decimal-digit 
                decimal-digits decimal-digit 
            */
            decimal_digits.Definition.Rules = new Rule.Stream
            {
                () => decimal_digit + decimal_digits[true]
            };

            /*
            decimal-digit:: one of
                0 1 2 3 4 5 6 7 8 9 
            */
            decimal_digit.Definition.Rules = new Rule.Stream
            {
                () => '0',
                () => '1',
                () => '2',
                () => '3',
                () => '4',
                () => '5',
                () => '6',
                () => '7',
                () => '8',
                () => '9'
            };

            Terminal[] _ = { '(', ')', '+', '-', '*', '/' };

            Nonterminal E = "E";
            Nonterminal Eopt = "E'";

            Nonterminal T = "T";
            Nonterminal Topt = "T'";

            Nonterminal F = "F";
            Nonterminal N = "N";

            E.Definition.Rules = new Rule.Stream
            {
                () => T[false] + Eopt[true]
            };

            T.Definition.Rules = new Rule.Stream
            {
                { () => F[false] + Topt[true] }
            };

            F.Definition.Rules = new Rule.Stream
            {
                { () => N[false] },
                { () => '(' + E[false] + ')' }
            };

            N.Definition.Rules = new Rule.Stream
            {
                { () => real_literal },
            };

            Eopt.Definition.Rules = new Rule.Stream
            {
                { () => '+' + E[false] },
                { () => '-' + E[false] },
            };

            Topt.Definition.Rules = new Rule.Stream
            {
                { () => '*' + T[false] },
                { () => '/' + T[false] },
            };

            Func<double, double, double> add = (a, b) => a + b;
            Func<double, double, double> sub = (a, b) => a - b;
            Func<double, double, double> mul = (a, b) => a * b;
            Func<double, double, double> div = (a, b) => a / b;

            real_literal.Definition.Eval = (input, tree) =>
            {
                return double.Parse(tree.Context.Text, CultureInfo.InvariantCulture.NumberFormat);
            };

            Eopt.Definition.Eval = (a, b) =>
            {
                switch (b.Current)
                {
                    case '+':
                        return a + b;
                    case '-':
                        return a - b;
                    default:
                        throw new NotImplementedException("Input token is not recognized");
                }
            };

            Topt.Definition.Eval = (a, b) =>
            {
                switch (b.Current)
                {
                    case '*':
                        return a + b;
                    case '/':
                        return a / b;
                    default:
                        throw new NotImplementedException("Input token is not recognized");
                }
            };

            Nonterminal grammar = "G";

            grammar.Definition.Rules = new Rule.Stream 
            {
                () => E[false] + Terminal.Null
            };

            return grammar;
        }

        [TestMethod]
        public void TestGrammar1()
        {
            IToken target = CreateGrammar1();

            Input input = "1+(2/3)";

            target.Parse(input);

            string text = input.Text;

            double expected = 1.0f + (2.0f / 3.0f);

            double actual = TreeHelper.Eval(0, input.Tree);

            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void TestGrammar2()
        {
            IToken target = CreateGrammar2();

            Input input = "1+(2/3)";

            target.Parse(input);

            string text = input.Text;

            double expected = 1.0f + (2.0f / 3.0f);

            double actual = TreeHelper.Eval(0, input.Tree);

            Assert.AreEqual(expected, actual);
        }
    }
}
